package com.foryou.swkj.core.util;

import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * Created by hou on 2017/6/22.
 */
public class ValidateUtil {

    public static boolean isEmpty(String... values){
        boolean flag = false;

        for (int i = 0; i < values.length; i++) {
            if(StringUtils.isEmpty(values[i])){
                flag = true;
                break;
            }
        }

        return flag;
    }

    public static String isBlankRetMsg(Map params){
        StringBuilder errorMessage = new StringBuilder();

        for (Object o : params.entrySet()) {
            Map.Entry en = (Map.Entry)o;
            Object value = en.getValue();
            if(value instanceof String){

                if(StringUtils.isBlank(String.valueOf(value))){
                    errorMessage.append(String.format("[%s为空!]",en.getKey()));
                }
            }else if(value instanceof String[]){
                String[] values = (String[])value;
                if(values.length == 0 || StringUtils.isBlank(values[0])){
                    errorMessage.append(String.format("[%s为空!]",en.getKey()));
                }
            }else if(null == value){
                errorMessage.append(String.format("[%s为空!]",en.getKey()));
            }
        }

        return errorMessage.toString();
    }



    public static String genSmsValidateCode(){
        String[] beforeShuffle = new String[] { "1","2", "3", "4", "5", "6", "7",
                "8", "9", "0" };
        List list = Arrays.asList(beforeShuffle); //将数组转成List
        Collections.shuffle(list);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            sb.append(list.get(i));
        }
        String afterShuffle = sb.toString();
        String result = afterShuffle.substring(3, 9);
        return result;
    }

    public static void main(String[] args) {
        System.out.println(ValidateUtil.genSmsValidateCode());
        System.out.println(StringUtils.prependIfMissingIgnoreCase("aaa","32132","DJ","DK"));
    }

}
